#include "vtor_menu.h"


#include "main.h"
#include "tim.h"
#include "schedule.h"
#include "string.h"

#include "oled.h"
#include "vtor_timer.h"

// 需要引用的外部变量或成员
extern struct VtorTimer* pTSBase;
extern struct VtorTimer* pTS1ms;
extern struct VtorTimer* pTS5ms;
extern struct VtorTimer* pTS10ms;
extern struct VtorTimer* pTS100ms;
extern struct VtorTimer* pTS1000ms;


// 内部变量和成员
struct VtorMenu vtorMenu[] = {
	{"VtorMenu",	0,	{0},	NULL,	NULL,	0,	0},
		{"TimInfo",	0,	{0},	NULL,	NULL,	0,	0},
			{"Tim2Info",	0,	{0},	NULL,	NULL,	0,	0},
				{"T2OC1",	0,	{0},	NULL,	NULL,	0,	0},
				{"T2OC2",	0,	{0},	NULL,	NULL,	0,	0},
				{"T2OC3",	0,	{0},	NULL,	NULL,	0,	0},
				{"T2OC4",	0,	{0},	NULL,	NULL,	0,	0},
			{"Tim3Info",	0,	{0},	NULL,	NULL,	0,	0},
				{"T3IC1",	0,	{0},	NULL,	NULL,	0,	0},
				{"T3IC2",	0,	{0},	NULL,	NULL,	0,	0},
				{"T3IC3",	0,	{0},	NULL,	NULL,	0,	0},
				{"T3IC4",	0,	{0},	NULL,	NULL,	0,	0},
		{"TaskInfo",	0,	{0},	NULL,	NULL,	0,	0},
			{"TaskRunCnt",	0,	{0},	NULL,	NULL,	0,	0},
				{"T1RC",	0,	{0},	NULL,	NULL,	0,	0},
				{"T2RC",	0,	{0},	NULL,	NULL,	0,	0},
				{"T3RC",	0,	{0},	NULL,	NULL,	0,	0},
				{"T4RC",	0,	{0},	NULL,	NULL,	0,	0},
				{"T5RC",	0,	{0},	NULL,	NULL,	0,	0},
			{"TaskRunTime",	0,	{0},	NULL,	NULL,	0,	0},
				{"T1RT",	0,	{0},	NULL,	NULL,	0,	0},
				{"T2RT",	0,	{0},	NULL,	NULL,	0,	0},
				{"T3RT",	0,	{0},	NULL,	NULL,	0,	0},
				{"T4RT",	0,	{0},	NULL,	NULL,	0,	0},
				{"T5RT",	0,	{0},	NULL,	NULL,	0,	0},
		{"About",	0,	{0},	NULL,	NULL,	0,	0},
			{"vtor3478",	0,	{0},	NULL,	NULL,	0,	0},
			{"@qq.com",	0,	{0},	NULL,	NULL,	0,	0},
			{"Log",	0,	{0},	NULL,	NULL,	0,	0},
};
struct VtorMenu* pRootMenu			= NULL;
struct VtorMenu* pTimInfoMenu		= NULL;
struct VtorMenu* pT2InfoMenu		= NULL;
struct VtorMenu* pT2OC1Menu			= NULL;
struct VtorMenu* pT2OC2Menu			= NULL;
struct VtorMenu* pT2OC3Menu			= NULL;
struct VtorMenu* pT2OC4Menu			= NULL;
struct VtorMenu* pT3InfoMenu		= NULL;
struct VtorMenu* pT3IC1Menu			= NULL;
struct VtorMenu* pT3IC2Menu			= NULL;
struct VtorMenu* pT3IC3Menu			= NULL;
struct VtorMenu* pT3IC4Menu			= NULL;
struct VtorMenu* pTaskInfoMenu		= NULL;
struct VtorMenu* pTaskRunCntMenu			= NULL;
struct VtorMenu* pT1RCMenu				= NULL;
struct VtorMenu* pT2RCMenu				= NULL;
struct VtorMenu* pT3RCMenu				= NULL;
struct VtorMenu* pT4RCMenu				= NULL;
struct VtorMenu* pT5RCMenu				= NULL;
struct VtorMenu* pTaskRunTimeMenu		= NULL;
struct VtorMenu* pT1RTMenu			= NULL;
struct VtorMenu* pT2RTMenu			= NULL;
struct VtorMenu* pT3RTMenu			= NULL;
struct VtorMenu* pT4RTMenu			= NULL;
struct VtorMenu* pT5RTMenu			= NULL;
struct VtorMenu* pAboutMenu			= NULL;
struct VtorMenu* pCreaterMenu		= NULL;
struct VtorMenu* pEmailMenu			= NULL;
struct VtorMenu* pLogMenu			= NULL;

__weak void VtorMenu_DisplayItem(uint8_t itemIdx, struct VtorMenu* pCurMenuItem)
{
	unsigned char oldOledPaintMode = pOledPaint->mode;
	unsigned char displayBuffer[50] = {0};
	sprintf((char *)displayBuffer," %s:%d\r\n",
			pCurMenuItem->name,
			pCurMenuItem->value);
	// 如果当前菜单所选项刚好是当前项，特殊显示
	if (pCurVtorMenu->itemIdx == itemIdx)
	{
		// 被选中的项，使用反色，前面添加字符
		pOledPaint->mode = OLED_PAINT_MODE_INVERT;
		displayBuffer[0] = '>';
	}
	//HAL_UART_Transmit(&huart1, displayBuffer, strlen(displayBuffer), 0xffff);
	OLED_ShowString(0 , itemIdx * 8, displayBuffer);
	pOledPaint->mode = oldOledPaintMode;
}

void VtorMenu_Handler(struct VtorMenu* pCurMenu)
{
	if (pRootMenu == pCurMenu)
	{
	}
}


void VtorMenu_DisplayLogItem(uint8_t itemIdx, struct VtorMenu* pLogItem)
{
	if (pLogItem == pCurVtorMenu)
	{
		OLED_RefreshLog();
	}
	else
	{
		VtorMenu_DisplayItem(itemIdx, pLogItem);
	}
}

// 菜单预处理，用于操作之前，主要是为了给菜单项进行赋值，方便后面显示（可为空）
void VtorMenu_PreProcess()
{
	pRootMenu->value = VtorTimer_GetTick();

	pT2OC1Menu->value = __HAL_TIM_GetCompare(&htim2,TIM_CHANNEL_1);
	pT2OC2Menu->value = __HAL_TIM_GetCompare(&htim2,TIM_CHANNEL_2);
	pT2OC3Menu->value = __HAL_TIM_GetCompare(&htim2,TIM_CHANNEL_3);
	pT2OC4Menu->value = __HAL_TIM_GetCompare(&htim2,TIM_CHANNEL_4);

	pT3IC1Menu->value = __HAL_TIM_GetCompare(&htim3,TIM_CHANNEL_1);
	pT3IC2Menu->value = __HAL_TIM_GetCompare(&htim3,TIM_CHANNEL_2);
	pT3IC3Menu->value = __HAL_TIM_GetCompare(&htim3,TIM_CHANNEL_3);
	pT3IC4Menu->value = __HAL_TIM_GetCompare(&htim3,TIM_CHANNEL_4);


}
// 菜单再处理，用于操作之后，主要是为了将菜单项值赋值给变量，便于实际操控（可为空）
void VtorMenu_ReProcess()
{

	__HAL_TIM_SetCompare(&htim2,TIM_CHANNEL_1,pT2OC1Menu->value);
	__HAL_TIM_SetCompare(&htim2,TIM_CHANNEL_2,pT2OC2Menu->value);
	__HAL_TIM_SetCompare(&htim2,TIM_CHANNEL_3,pT2OC3Menu->value);
	__HAL_TIM_SetCompare(&htim2,TIM_CHANNEL_4,pT2OC4Menu->value);

}

void VtorMenu_Init()
{
	unsigned char menuIdx = 0;
	pRootMenu			= &vtorMenu[menuIdx++];

	pTimInfoMenu		= &vtorMenu[menuIdx++];
	pT2InfoMenu			= &vtorMenu[menuIdx++];
	pT2OC1Menu			= &vtorMenu[menuIdx++];
	pT2OC2Menu			= &vtorMenu[menuIdx++];
	pT2OC3Menu			= &vtorMenu[menuIdx++];
	pT2OC4Menu			= &vtorMenu[menuIdx++];

	pT3InfoMenu			= &vtorMenu[menuIdx++];
	pT3IC1Menu			= &vtorMenu[menuIdx++];
	pT3IC2Menu			= &vtorMenu[menuIdx++];
	pT3IC3Menu			= &vtorMenu[menuIdx++];
	pT3IC4Menu			= &vtorMenu[menuIdx++];

	pTaskInfoMenu		= &vtorMenu[menuIdx++];

	pTaskRunCntMenu		= &vtorMenu[menuIdx++];
	pT1RCMenu			= &vtorMenu[menuIdx++];
	pT2RCMenu			= &vtorMenu[menuIdx++];
	pT3RCMenu			= &vtorMenu[menuIdx++];
	pT4RCMenu			= &vtorMenu[menuIdx++];
	pT5RCMenu			= &vtorMenu[menuIdx++];

	pTaskRunTimeMenu	= &vtorMenu[menuIdx++];
	pT1RTMenu			= &vtorMenu[menuIdx++];
	pT2RTMenu			= &vtorMenu[menuIdx++];
	pT3RTMenu			= &vtorMenu[menuIdx++];
	pT4RTMenu			= &vtorMenu[menuIdx++];
	pT5RTMenu			= &vtorMenu[menuIdx++];

	pAboutMenu			= &vtorMenu[menuIdx++];
	pCreaterMenu		= &vtorMenu[menuIdx++];
	pEmailMenu			= &vtorMenu[menuIdx++];
	pLogMenu			= &vtorMenu[menuIdx++];
	// 构建菜单
	VtorMenu_RelateItem(pRootMenu,pRootMenu);

	VtorMenu_RelateItem(pRootMenu,pTimInfoMenu);
	VtorMenu_RelateItem(pRootMenu,pTaskInfoMenu);
	VtorMenu_RelateItem(pRootMenu,pAboutMenu);

	VtorMenu_RelateItem(pTimInfoMenu,pT2InfoMenu);
	VtorMenu_RelateItem(pT2InfoMenu,pT2OC1Menu);
	VtorMenu_RelateItem(pT2InfoMenu,pT2OC2Menu);
	VtorMenu_RelateItem(pT2InfoMenu,pT2OC3Menu);
	VtorMenu_RelateItem(pT2InfoMenu,pT2OC4Menu);

	VtorMenu_RelateItem(pTimInfoMenu,pT3InfoMenu);
	VtorMenu_RelateItem(pT3InfoMenu,pT3IC1Menu);
	VtorMenu_RelateItem(pT3InfoMenu,pT3IC2Menu);
	VtorMenu_RelateItem(pT3InfoMenu,pT3IC3Menu);
	VtorMenu_RelateItem(pT3InfoMenu,pT3IC4Menu);

	VtorMenu_RelateItem(pTaskInfoMenu,pTaskRunCntMenu);
	VtorMenu_RelateItem(pTaskInfoMenu,pTaskRunTimeMenu);

	VtorMenu_RelateItem(pTaskRunCntMenu,pT1RCMenu);
	VtorMenu_RelateItem(pTaskRunCntMenu,pT2RCMenu);
	VtorMenu_RelateItem(pTaskRunCntMenu,pT3RCMenu);
	VtorMenu_RelateItem(pTaskRunCntMenu,pT4RCMenu);
	VtorMenu_RelateItem(pTaskRunCntMenu,pT5RCMenu);

	VtorMenu_RelateItem(pTaskRunTimeMenu,pT1RTMenu);
	VtorMenu_RelateItem(pTaskRunTimeMenu,pT2RTMenu);
	VtorMenu_RelateItem(pTaskRunTimeMenu,pT3RTMenu);
	VtorMenu_RelateItem(pTaskRunTimeMenu,pT4RTMenu);
	VtorMenu_RelateItem(pTaskRunTimeMenu,pT5RTMenu);

	VtorMenu_RelateItem(pAboutMenu,pCreaterMenu);
	VtorMenu_RelateItem(pAboutMenu,pEmailMenu);
	VtorMenu_RelateItem(pAboutMenu,pLogMenu);

	pLogMenu->display = VtorMenu_DisplayLogItem;

	pCurVtorMenu = pRootMenu;
	pCurVtorMenu = pLogMenu;
}

